package capstoneproject.dudiachi.Main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import capstoneproject.dudiachi.Model.Province;

public class ProvinceGridCellAdapter extends BaseAdapter implements OnClickListener{

	private Context context;
	private ArrayList<Province> list;
	private ArrayList<Bitmap> bitmap;
	private GridItems[] items;
	
	public ProvinceGridCellAdapter(Context applicationContext,
			ArrayList<Province> provinceList, ArrayList<Bitmap> bitArray, GridItems[] gridItems) {
		this.context = applicationContext;
		this.list = provinceList;
		this.bitmap = bitArray;
		this.items = gridItems;
	}

	public GridItems[] getItems() {
		return items;
	}
	
	public class ViewHolder{
		public ImageView image;
		public TextView name;
		
	}
	
	public void setItems(GridItems[] items) {
		this.items = items;
	}
	
	@Override
	public int getCount() {
		return items.length;
	}

	@Override
	public Object getItem(int pos) {
		if(items != null && pos >= 0 && pos < getCount()) {
			return items[pos];
		}
		return null;
	}

	@Override
	public long getItemId(int pos) {
		if(items != null && pos >= 0 && pos < getCount()) {
			return items[pos].id;
		}
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder viewHolder;
		if(v == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.place_province_grid_cell, parent, false);
		}
		viewHolder = new ViewHolder();
		viewHolder.image = (ImageView) v.findViewById(R.id.image);
		viewHolder.name = (TextView) v.findViewById(R.id.name);

		RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.layout);
		layout.setTag("" + list.get(position).getProvinceID());
		layout.setOnClickListener(this);
		GridItems gridItems = items[position];
		setImage(position, viewHolder, gridItems.itemID);
		return v;
	}

	private void setImage(int pos, ViewHolder viewHolder, int itemID){
		viewHolder.image.setImageBitmap(bitmap.get(pos));
		viewHolder.image.setOnClickListener(this);
		viewHolder.image.setTag("" + list.get(itemID).getProvinceID());
		viewHolder.name.setText(list.get(itemID).getProvinceName());
	}
	
	@Override
	public void onClick(View v) {
		if(BaseActivity.POS == 1){
			String tag = (String) v.getTag();
			Intent intent = new Intent(context, FestivalActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			Bundle bundle = new Bundle();
			bundle.putSerializable("list", list);
			bundle.putString("tag", "fromFestivalProvince");
			bundle.putString("id", tag);
			intent.putExtras(bundle);
			context.startActivity(intent);
		}
		if (BaseActivity.POS == 2) {
			String tag = (String) v.getTag();
			Intent intent = new Intent(context, PlaceActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			Bundle bundle = new Bundle();
			bundle.putSerializable("list", list);
			bundle.putString("tag", "fromProvince");
			bundle.putString("id", tag);
			intent.putExtras(bundle);
			context.startActivity(intent);
		}
	}

}
